package com.ruoyi.wq.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wq.mapper.WqQuestionMapper;
import com.ruoyi.wq.mapper.WqQuestionReplyMapper;
import com.ruoyi.wq.domain.WqQuestion;
import com.ruoyi.wq.domain.WqQuestionReply;
import com.ruoyi.wq.domain.vo.WqQuestionVO;
import com.ruoyi.wq.service.IWqQuestionService;

/**
 * 用户提问Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-04
 */
@Service
public class WqQuestionServiceImpl extends ServiceImpl<WqQuestionMapper, WqQuestion> implements IWqQuestionService {

    @Autowired
    private WqQuestionMapper wqQuestionMapper;
    
    @Autowired
    private WqQuestionReplyMapper wqQuestionReplyMapper;

    /**
     * 查询用户提问
     * 
     * @param id 用户提问主键
     * @return 用户提问
     */
    @Override
    public WqQuestion selectWqQuestionById(String id)
    {
        return wqQuestionMapper.selectWqQuestionById(id);
    }

    /**
     * 查询用户提问列表
     * 
     * @param wqQuestion 用户提问
     * @return 用户提问
     */
    @Override
    public List<WqQuestion> selectWqQuestionList(WqQuestion wqQuestion)
    {
        return wqQuestionMapper.selectWqQuestionList(wqQuestion);
    }

    /**
     * 新增用户提问
     * 
     * @param wqQuestion 用户提问
     * @return 结果
     */
    @Override
    public int insertWqQuestion(WqQuestion wqQuestion)
    {
        wqQuestion.setCreateTime(DateUtils.getNowDate());
        return wqQuestionMapper.insertWqQuestion(wqQuestion);
    }

    /**
     * 修改用户提问
     * 
     * @param wqQuestion 用户提问
     * @return 结果
     */
    @Override
    public int updateWqQuestion(WqQuestion wqQuestion)
    {
        wqQuestion.setUpdateTime(DateUtils.getNowDate());
        return wqQuestionMapper.updateWqQuestion(wqQuestion);
    }

    /**
     * 批量删除用户提问
     * 
     * @param ids 需要删除的用户提问主键
     * @return 结果
     */
    @Override
    public int deleteWqQuestionByIds(String[] ids)
    {
        return wqQuestionMapper.deleteWqQuestionByIds(ids);
    }

    /**
     * 删除用户提问信息
     * 
     * @param id 用户提问主键
     * @return 结果
     */
    @Override
    public int deleteWqQuestionById(String id)
    {
        return wqQuestionMapper.deleteWqQuestionById(id);
    }

	@Override
	public TableDataInfo getPageList(WqQuestionVO wqQuestion) {
		IPage<WqQuestionVO> page = baseMapper.selectPageList(Condition.getPage(wqQuestion), wqQuestion);
		for(WqQuestionVO vo : page.getRecords()){
			vo.setMy(true);
			// 不是自己显示匿名
			if(wqQuestion.getSendUserId() == null){
				vo.setMy(false);
				vo.setAvatar(null);
			}else{
				if(!vo.getSendUserId().equals(wqQuestion.getSendUserId())){
					vo.setMy(false);
					vo.setAvatar(null);
				}
			}
			if(vo.getMy()){
				QueryWrapper<WqQuestionReply> qw = new QueryWrapper<>();
				WqQuestionReply entity = new WqQuestionReply();
				entity.setQuestionId(vo.getId());
				qw.setEntity(entity);
				qw.orderByAsc("create_time");
				List<WqQuestionReply> replyList = wqQuestionReplyMapper.selectList(qw);
				vo.setReplyList(replyList);
			}
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public TableDataInfo getMyPageList(WqQuestionVO wqQuestion) {
		IPage<WqQuestionVO> page = baseMapper.selectPageList(Condition.getPage(wqQuestion), wqQuestion);
		for(WqQuestionVO vo : page.getRecords()){
			/*if("0".equals(vo.getCheckStatus())){
				vo.setSendUserId(null);
				vo.setAvatar(null);
				vo.setNickName(null);
			}*/
			
			QueryWrapper<WqQuestionReply> qw = new QueryWrapper<>();
			WqQuestionReply entity = new WqQuestionReply();
			entity.setQuestionId(vo.getId());
			qw.setEntity(entity);
			qw.orderByAsc("create_time");
			List<WqQuestionReply> replyList = wqQuestionReplyMapper.selectList(qw);
			vo.setReplyList(replyList);
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}
}
